package org.pupil.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 拦截器的加载器
 * @Author: pupil
 * @Date: 2024/07/17 下午 5:56
 */
public class FilterLoader {

    // 拦截器集合
    private List<Filter> filters = new ArrayList<>();

    // 添加拦截器
    public void addFilter(Filter filter){
        filters.add(filter);
    }

    // 批量添加拦截器
    public void addFilter(List<Filter> filters){
        for (Object filter : filters) {
            addFilter((Filter) filter);
        }
    }

    // 处理拦截数据
    public FilterResponse doFilter(FilterData data){
        for (Filter filter : filters) {
           final FilterResponse filterResponse = filter.doFilter(data);
           // 当返回结果为错误时返回结果
            if (!filterResponse.getResult()) {
                return filterResponse;
            }
        }
        return new FilterResponse(true,null);
    }
}
